<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Symbol实现对象属性保护</title>
</head>
<body>
<script>
    /**
     * Symbol 属于新增的基本类型
     * 作用： 私有化 -- 保护对象某些属性值不被外部读取,实现了属性保护不被外部读取
    */
    let s1 = Symbol();
    let s2 = Symbol();
    console.log(s1 == s2); // false

    let s3 = Symbol('alan');
    let s4 = Symbol('alan');
    console.log(s3 == s4); // false
    let Person = (function(){
        // 生成一个KEY名称
        var _gender = Symbol('gender');
        function P(name,gender) {
            this.name = name;
            // Symbol(gender)赋值
            this[_gender] = gender;
        }
        P.prototype.sex = function() {
            return this[_gender];
        }
        return P;
    })();
    let P1 = new Person('小刘','男');
    console.log(P1);
    console.log(P1.sex());// 构造函数能够访问自己Symbol生成KEY值的属性
    console.log(P1.Symbol('gender'));// 这样是访问不了的
    console.log(P1[Symbol('gender')]);// 这样是访问不了的
</script>
</body>
</html>

